//创建载具和建筑
//所有都加入动态模拟
params ["_type", "_offset", "_dir", "_crew", "_damage", "_fuel", "_pos", "_group"];

private _pos_temp = [];

_pos_temp pushBack ((_pos select 0) + (_offset select 0));
_pos_temp pushBack ((_pos select 1) + (_offset select 1));

//是载具
if(_type isKindOf "AllVehicles") then
{	
	_pos_temp pushBack ((_offset select 2) + 0.8);
}
else
{	
	_pos_temp pushBack ((_offset select 2));
};

//类型修正
if(Param_E_Veh == 1
&& {!_crew}) then
{
	if(_type isKindOf "Tank") then
	{
		if(random 99 < 50) then
		{
			_type = selectRandom as_veh_light;
		}
		else
		{
			_type = selectRandom as_veh_transport;
		};
	}
	else
	{
		if(_type isKindOf "Car") then
		{
			_type = selectRandom as_veh_transport;
		}
		else
		{
		};
	};
};

//模组飞机类型修正
if(Param_RHS == 1)then
{
	//直升机替换
	if(_type isKindOf "Helicopter"
	&& {random 100 < 50}) then
	{
		if(_type == "B_Heli_Light_01_F")then{
			//小鸟
			_type = "RHS_MELB_MH6M";
		};
		if(_type == "C_Heli_Light_01_civil_F")then{
			//平民小鸟
			_type = "RHS_MELB_H6M";
		};
		if(_type == "B_Heli_Light_01_dynamicLoadout_F")then{
			//武装小鸟
			_type = "RHS_MELB_AH6M";
		};
		if(_type == "B_Heli_Attack_01_dynamicLoadout_F")then{
			//AH99
			_type = selectRandom ["RHS_AH64DGrey", "RHS_AH64D"];
		};
		if(_type == "B_Heli_Transport_01_F")then{
		//鬼鹰
			_type = "RHS_UH60M_d";
		};
		if(_type == "I_Heli_light_03_dynamicLoadout_F")then{
		//WY-55
			_type = selectRandom["rhsgref_cdf_Mi24D","rhsgref_cdf_Mi24D_early","rhsgref_mi24g_CAS","rhsgref_cdf_Mi35"];
		};
		if(_type == "I_Heli_light_03_unarmed_F")then{
		//WY-55 未武装
			_type = selectRandom["rhsgref_cdf_reg_Mi8amt","rhsgref_cdf_reg_Mi17Sh"];
		};
		if(_type == "I_Heli_Transport_02_F")then{
		//CH-49
			_type = selectRandom["rhsgref_cdf_reg_Mi8amt","rhssaf_airforce_ht40","rhssaf_airforce_ht48"];
		};
		if(_type == "B_Heli_Transport_03_F")then{
		//支奴干
			_type = "RHS_CH_47F_10";
		};	
	};
};

private _veh = createVehicle [_type, [0,0,0] , [], 0, "NONE"];//createSimpleObject [_type, _pos_temp];//
_veh setDir _dir;
_veh setVehiclePosition [_pos_temp, [], 0, "CAN_COLLIDE"];

if(_type isKindOf "House") then
{
	_veh setVectorUp [0,0,1];
};
//是载具
if(_type isKindOf "AllVehicles") then
{	
	_veh setVariable["no_cleanup", true];//不被清理标记
	//随机迷彩
	_veh call fn_randomMask;
	if(_crew) then
	{	
		//创建乘员
		createVehicleCrew _veh;
		private _crews = crew _veh; 
		{
			[_x] call fn_initUnit;
			[_x] joinSilent _group;
		}forEach _crews;
		
		
		//成员数为1
		if(count _crews == 1) then
		{//为1的一般是固定武器和无人机
			if(count _crews > 0 && {random 100 < Param_E_K}) then
			{
				[(_crews select 0)] call fn_eventSupport;
				/*
				//如果是无人机机枪等，移除handleDamage事件
				if(_type find "_UAV_AI_" >= 0) then
				{
					{
						_x removeAllEventHandlers "HandleDamage";
						_x removeAllEventHandlers "hitPart";
					}forEach _crews;
				};
				*/
			}
			else
			{
				//moveOut (_crews select 0);
				(_crews select 0) leaveVehicle _veh;
			};
		}
	}
	else
	{
		//空载具弹药修正（不是飞机）
		if(_type isKindOf "Tank") then
		{
			/*
			private _list_mag = [];
			//先移除
			{
				_list_mag pushBack [_x select 0,_x select 1,
				(_x select 2) * Param_E_Veh_Ammo / 100];
				
				_veh removeMagazinesTurret [_x select 0,_x select 1];
			}forEach (magazinesAllTurrets _veh);
			
			//再添加
			{
				_veh addMagazineTurret [_x select 0, _x select 1, _x select 2];
			}forEach _list_mag;
			*/
			_veh setVehicleAmmo (Param_E_Veh_Ammo / 100);
		};
		

	};
	//禁止绿军玩家使用点的载具
	_veh setVariable ["green_disable", true];
	//添加事件，红军上车则取消禁用（上车后也会可以被清理）
	_veh addEventHandler ["GetIn", {
		params ["_veh", "_role", "_unit", "_turret"];
		if(isPlayer _unit && {_unit call fn_isPlayerRed})then{
			_veh setVariable ["green_disable", false];
			_veh removeAllEventHandlers "GetIn";
			_veh setVariable["no_cleanup", false];//取消不被清理标记
		};
	}];
	
	//死亡事件 快速清除
	_veh addEventHandler ["killed", {
		(_this select 0) spawn {
			Sleep 45;
			deleteVehicle _this;
		};
	}];

}
else
{
	//墙壁、沙袋等 替换为简单对象
	if(_type find "Wall" >= 0
	|| {_type find "BagFence" >= 0}
	|| {_type find "SandbagBarricade" >= 0}
	|| {_type find "BagBunker" >= 0}) then
	{
		private _position = getPosWorld _veh;
		private _vectorDirUp = [vectorDir _veh, vectorUp _veh];
		private _model = getModelInfo _veh select 1;
		deleteVehicle _veh;
		_veh = createSimpleObject [_model, _position];
		_veh setVectorDirAndUp _vectorDirUp;
		//给予回收价值 简单对象无效
		//_veh setVariable["res_num", 15, true];
	}
	else
	{
		//给予回收价值
		if(_type != "MapBoard_altis_F"//不是地图
		&& {_type find "Tower" < 0}
		&& {_type find "Dome" < 0}
		&& {_type find "Patrol" < 0}
		&& {!(_type isKindOf "House")}) then
		{
			_veh setVariable["res_num", 8, true];
		};
	};

};

_veh setDamage _damage;
_veh setFuel _fuel;
_veh enableDynamicSimulation true;

//（y轴是北方）




//地图可以被查看标记
if(typeOf _veh == "MapBoard_altis_F")then
{
	_veh setVariable["viewed", false, true];
};

//血袋产生毒气
if(typeOf _veh == "Land_BloodBag_F")then
{
	[getPos _veh] spawn fn_toxicKeep;
};

//白板设置纹理
if(typeOf _veh == "Land_Noticeboard_F")then
{
	_veh setObjectTextureGlobal  [0, format["images\story\b%1.paa", (floor random 7) + 1]];
};



_veh
